
var express = require('express');
var router = express.Router();
// 数据库
var userDAO = require("../bin/cn/userDAO.js");
var logDAO = require("../bin/cn/logDAO.js");
var replyDAO = require("../bin/cn/replyDAO.js");
var diaryDAO = require("../bin/cn/diaryDAO.js");
var wordDAO = require("../bin/cn/word.js");
// 导入时间函数
var newtime = require("../bin/libs/time.js");
// 判断是否登录
var isLogin = require("../bin/libs/isLogin.js");

// 回复帖子详情
router.get("/add",isLogin);
router.get("/add",(req,res)=>{
    logDAO.findOne({_id:req.query.logID})
    .populate('userID')
    .then(data=>{
        if(!data){
            res.json({err:1,msg:"帖子已经找不到"});
            return;
        }else{
            req.query.time = newtime();
            req.query.userID = req.session.user._id;
            var r = new replyDAO(req.query);
            r.save()
            .then(data1=>{
                data.reply.push(data1._id);
                var d = new logDAO(data);
                d.save()
                .then(d=>{
                    res.json({err:0,msg:"回复成功"});
                    if(req.session.user._id==data.userID._id){
                        return;
                    }
                    var w =new wordDAO({
                        Yuser:req.session.user._id,
                        Muser:data.userID._id,
                        logID:req.query.logID,
                        time:req.query.time,
                        num:1,
                        read:1,
                    })
                    w.save()
                    .then(word=>{
                        console.log("消息保存了")
                    });
                })
            })
        }
    }) 
});

// 删除一个日记
var fs = require("fs");
router.get("/del",isLogin)
router.get("/del",(req,res)=>{
    logDAO.findOne({_id:req.query.id})
    .populate("userID",{psw:0})
    .then(data=>{
        if(data.userID._id==req.session.user._id){
            logDAO.remove({_id:req.query.id})
            .then(datas=>{
                res.json({err:0,msg:"删除成功！"});
                replyDAO.remove({logID:req.query.id})
                .then(sss=>{
                    console.log("回复删除成功")
                });
                if(data.img){
                    fs.unlinkSync('./public'+data.img);
                }
            })
            .catch(a=>{
                res.json({err:1,msg:"帖子不存在了！"})
            })
        }else{
            res.json({err:2,msg:"不是自己的帖子！"})
        }
    })
});

// 修改日记(前)
router.get("/update",isLogin);
router.get("/update",(req,res)=>{
    logDAO.findOne({_id:req.query.id})
    .then(data=>{
        diaryDAO.find({userID:data.userID},{name:1})
        .then(data1=>{
            res.json({err:0,data:{data:data,data1:data1}});
        })
    })
    .catch(err=>{
        res.json({err:2,msg:"页面不存在"});
    })
});

// 修改日记(后)
router.post("/update",isLogin);
router.post("/update",(req,res)=>{
    logDAO.findOne({_id:req.body.id})
    .then(data=>{
        if(data.userID==req.session.user._id){
            logDAO.update({_id:req.body.id},{$set:{
                content:req.body.content,
                diaryID:req.body.diaryID,
            }})
            .then(datas=>{
                res.json({err:0,msg:"修改成功"});
            })
        }else{
            res.json({err:1,msg:"请修改自己的日记"});
        }
    })
    .catch(err=>{
        res.json({err:2,msg:"页面不存在"});
    })
});

// 查看有无消息
router.get("/word",isLogin);
router.get("/word",(req,res)=>{
    wordDAO.find({Muser:req.session.user._id,read:1})
    .then(data=>{
        res.json({err:0,data:data});
    })
});

// 查看未读消息并不再提示
router.get("/canword",isLogin);
router.get("/canword",(req,res)=>{
    wordDAO.find({Muser:req.session.user._id})
    .populate("Yuser")
    .then(data=>{
        res.json({err:0,data:data});
        for(var i=0; i<data.length; i++){
            data[i].read = 0;
            data[i].save();
        }
    })
});

// 删除消息
router.get("/delword",isLogin);
router.get("/delword",(req,res)=>{
    if(req.query.id=="all"){
        wordDAO.remove({Muser:req.session.user._id})
        .then(data=>{
            res.json({err:0,msg:"全部删除"});
        })
    }else{
        wordDAO.remove({_id:req.query.id})
        .then(data=>{
            res.json({err:0,msg:"删除一个"});
        })
    }
});

// 是否进入回复
router.post("/reply/replys",isLogin)
router.post("/reply/replys",(req,res)=>{

    if(req.body.repID==req.session.user._id){
        res.json({err:1,msg:"不能回复自己"});
    }else{
        userDAO.findOne({_id:req.body.repID})
        .then(data=>{
            res.json({err:0,data:data});
            
        })
    }
})

module.exports = router;